package com.pan.service.impl;

import com.pan.dao.goods.GoodsSelectDao;
import com.pan.dao.goodsImg.GoodsImgsDao;
import com.pan.dao.goodsImg.GoodsImgsDeleteDao;
import com.pan.dao.goodsImg.GoodsImgsSelectDao;
import com.pan.dao.goodsImg.GoodsImgsUpdateDao;
import com.pan.entity.Goods;
import com.pan.entity.GoodsImgs;
import com.pan.entity.Result;
import com.pan.enume.ResultEnum;
import com.pan.service.GoodsImgService;
import com.pan.tools.ConvertObject;
import com.pan.vo.GoodsImgsInsertVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;


@Service("goodsImgService")
public class GoodsImgServiceImpl implements GoodsImgService {


    @Resource
    private GoodsImgsSelectDao goodsImgsSelectDao;

    @Resource
    private GoodsImgsDeleteDao goodsImgsDeleteDao;

    @Resource
    private GoodsImgsUpdateDao goodsImgsUpdateDao;

    @Resource
    private GoodsSelectDao goodsSelectDao;

    @Resource
    private GoodsImgsDao goodsImgsDao;


    @Override
    public Result<List<GoodsImgs>> selectByGoodsId(Long goodsId){
        List<GoodsImgs> li = goodsImgsSelectDao.selectByGoodsId(goodsId);
        if (li==null||li.size()==0){
            return new Result(ResultEnum.NOT_FOUND_ERROR);
        }
        li = li.stream().sorted(Comparator.comparing(GoodsImgs::getImgSort)).collect(Collectors.toList());
        return new Result(ResultEnum.SUCCESS,li);
    }


    @Override
    public Result delete(String adminTicket,Long id){
        goodsImgsDeleteDao.deleteById(id);
        return new Result(ResultEnum.SUCCESS);
    }

    @Override
    public Result updateSort(String adminTicket,Long id,Integer imgSort){
        GoodsImgs goodsImgs = goodsImgsSelectDao.selectById(id);
        if (goodsImgs==null){
            return new Result(ResultEnum.NOT_FOUND_ERROR,id);
        }
        goodsImgsUpdateDao.updateImgSort(id, imgSort);
        return new Result(ResultEnum.SUCCESS);
    }

    @Override
    public Result insert(String adminTicket, GoodsImgsInsertVo goodsImgsInsertVo){
        Long goodsId = goodsImgsInsertVo.getGoodsId();
        Goods goods = goodsSelectDao.selectById(goodsId);
        if (goods==null){
            return new Result(ResultEnum.NOT_FOUND_ERROR);
        }
        GoodsImgs goodsImgs = (GoodsImgs) ConvertObject.convertObject(GoodsImgs.class,goodsImgsInsertVo);
        Long id = goodsImgsDao.insert(goodsImgs);
        return new Result(ResultEnum.SUCCESS,id);
    }

}


